package cn.wolfcode.wms.web.controller;


import cn.wolfcode.wms.domain.StockOutcomeBill;
import cn.wolfcode.wms.query.StockOutcomeBillQueryObject;
import cn.wolfcode.wms.service.IClientService;
import cn.wolfcode.wms.service.IDepotService;
import cn.wolfcode.wms.service.IStockOutcomeBillService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("stockOutcomeBill")
public class StockOutcomeBillController {
    @Autowired
    private IStockOutcomeBillService stockOutcomeBillService;
    @Autowired
    private IDepotService depotService;
    @Autowired
    private IClientService clientService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StockOutcomeBillQueryObject qo, Model model){
        PageResult pageResult = stockOutcomeBillService.queryResult(qo);
        model.addAttribute("result",pageResult);
        model.addAttribute("depots",depotService.selectList());
        model.addAttribute("clients",clientService.selectList());
        return "stockOutcomeBill/list";
    }
    @RequestMapping("audit")
    @ResponseBody
    public Object audit(Long id){
        JSONResult jsonResult = new JSONResult();
        try{
            stockOutcomeBillService.audit(id);

        }catch(Exception e){
            e.printStackTrace();;
            jsonResult.mark(e.getMessage());//把错误信息保存到JSONResult中
        }
        return jsonResult;
    }

    @RequestMapping("input")
    public String input(Model model,Long id){

        if (id != null) {
            StockOutcomeBill bill = stockOutcomeBillService.get(id);
            model.addAttribute("entity",stockOutcomeBillService.get(id));
        }
        model.addAttribute("depots",depotService.selectList());
        model.addAttribute("clients",clientService.selectList());
        return "stockOutcomeBill/input";
    }

    @RequestMapping("view")
    public String view(Model model,Long id){

        if (id != null) {
            StockOutcomeBill bill = stockOutcomeBillService.get(id);
            model.addAttribute("entity",stockOutcomeBillService.get(id));
        }
        StockOutcomeBill bill = stockOutcomeBillService.get(id);
        model.addAttribute("depot",depotService.get(bill.getDepot().getId()));
        model.addAttribute("client",clientService.get(bill.getClient().getId()));
        return "stockOutcomeBill/view";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){

        stockOutcomeBillService.delete(id);

        return new JSONResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StockOutcomeBill d){

        stockOutcomeBillService.saveOrUpdate(d);

        return new JSONResult();
    }

}
